package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JOptionPane;

import model.CarModel;
import view.ServerViewPanelCarModel;
import databaseController.DatabaseController;

public class ServerCarModelController {

	private ServerViewPanelCarModel panel;
	
	public ServerCarModelController() {
		this.panel = new ServerViewPanelCarModel();
		this.panel.getjButtonConfirm().addActionListener(new ListenerConfirm());
		this.panel.getjButtonClear().addActionListener(new ListenerClear());
	}
	
	public static Collection<CarModel> getCarModels() {
		return null;
	}
	
	public ServerViewPanelCarModel getPanel() {
		return this.panel;
	}
	
	private void clear() {
		this.panel.getjTextFieldCarModel().setText("");
		this.panel.getjTextFieldManufactorer().setText("");
		this.panel.getjTextFieldYear().setText("");		
	}
	
	class ListenerConfirm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Colocar registro de motorista na base de dados
			String carModel = ServerCarModelController.this.panel.getjTextFieldCarModel().getText();
			String manufactorer = ServerCarModelController.this.panel.getjTextFieldManufactorer().getText();
			String year = ServerCarModelController.this.panel.getjTextFieldYear().getText();
			
			DatabaseController.getInstance().addCarModel(new CarModel(carModel, year, manufactorer));
			
			JOptionPane.showMessageDialog(null,
				    "Modelo de carro cadastrado com sucesso!\n " , 
				    "Sucesso", JOptionPane.INFORMATION_MESSAGE);
			
			ServerCarModelController.this.clear();			
			
			System.out.println("*=*   Car Model   *=*");
			System.out.println("Car Model: " + carModel);
			System.out.println("Year: " + year);
			System.out.println("Manufactorer: " + manufactorer);
			System.out.println("*=*   ------------------   *=*");
		}
	}
	
	class ListenerClear implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ServerCarModelController.this.clear();
		}
	}	
}
